﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JMcms.Common;

namespace JMcms.Web.Plugin.Job.admin
{
    public partial class job_edit : JMcms.Web.UI.ManagePage
    {
        private string action = JMEnums.ActionEnum.Add.ToString(); //操作类型
        protected int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = JMRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == JMEnums.ActionEnum.Edit.ToString())
            {
                this.action = JMEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = JMRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.job().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("plugin_job_list", JMEnums.ActionEnum.View.ToString()); //检查权限
                if (action == JMEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }


        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.job bll = new BLL.job();
            Model.job model = bll.GetModel(_id);
            ddpExperience.SelectedValue = model.experience;
            txtJobTitle.Text = model.title;
            rblIsLock.SelectedValue = model.is_lock.ToString();
            txtSortId.Text = model.sort_id.ToString();
            txtAddTime.Text = string.Format("{0:yyyy-MM-dd}", model.add_time);
            txtSalary.Text = model.salary;
            txtAddress.Text = model.work_address;
            ddpEducation.SelectedValue = model.education;
            txtDescription.Value = model.description;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.job model = new Model.job();
            BLL.job bll = new BLL.job();
            model.title = txtJobTitle.Text.Trim();
            model.add_time = DateTime.Parse(this.txtAddTime.Text.Trim());
            model.salary = txtSalary.Text.Trim();
            model.education = ddpEducation.SelectedValue;
            model.work_address = txtAddress.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = int.Parse(rblIsLock.SelectedValue);
            model.description = txtDescription.Value;
            model.experience = ddpExperience.SelectedValue;

            if (bll.Add(model) > 0)
            {
                AddAdminLog(JMEnums.ActionEnum.Add.ToString(), "添加招聘内容：" + model.title); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.job bll = new BLL.job();
            Model.job model = bll.GetModel(_id);
            model.id = _id;
            model.title = txtJobTitle.Text.Trim();
            model.add_time = DateTime.Parse(this.txtAddTime.Text.Trim());
            model.description = txtDescription.Value;
            model.salary = txtSalary.Text.Trim();
            model.experience = ddpExperience.SelectedValue;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = int.Parse(rblIsLock.SelectedValue);
            model.work_address = txtAddress.Text.Trim();
            model.education = ddpEducation.SelectedValue;
            if (bll.Update(model))
            {
                AddAdminLog(JMEnums.ActionEnum.Edit.ToString(), "修改招聘内容：" + model.title); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == JMEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("plugin_job_list", JMEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误啦！",string.Empty);
                    return;
                }
                JscriptMsg("编辑成功！", "job_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("plugin_job_list", JMEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误啦！", string.Empty);
                    return;
                }
                JscriptMsg("添加成功！", "job_list.aspx");
            }
        }
    }
}